package com.itheima.sh.dao;

import com.itheima.sh.pojo.Order;
import com.itheima.sh.pojo.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface OrderMapper {
    //定义方法
    //需求：通过订单编号20140921003查询出**订单信息**，并查询出**下单人信息**。
    @Select("select * from tb_order where order_number=#{orderNumber}")
    /*
        @Results 相当于xml中的resultMap标签
     */
    @Results(
          value={
                  //声明tb_order表的主键
                  //id=true 表示是主键 column = "id" 表的主键列名 property = "id" Order实体类的主键的成员变量名
                  @Result(id=true,column = "id",property = "id"),
                  /*
                         1.property = "user" 表示在实体类Order中的user对象的成员变量名private User user;
                         2.javaType = User.class 表示成员变量名user所属类型
                         3.column = "user_id" 表示将上述sql语句：
                         select * from tb_order where order_number=#{orderNumber}
                         查询的用户user_id作为下述sql语句的入参。
                         select * from tb_user where id=#{id}

                   */
                  @Result(column = "user_id",property = "user",
                          javaType = User.class,one = @One(select = "com.itheima.sh.dao.UserMapper.queryUserById",
                                                        fetchType = FetchType.LAZY ))
          }

    )
    Order queryOneToOne(@Param("orderNumber") String orderNumber);

    //根据用户id查询订单信息 select * from tb_order where user_id=1;
    @Select("select * from tb_order where user_id=#{uid}")
    List<Order> queryOrdersByUserId(@Param("uid")Integer id);

}
